<?php
namespace Modules\Home\Controllers;

use \Common\Models\UserModel;
use \Common\Models\UserInfoModel;

class UserProfileController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('资料设置');

    $getUserSession = $this -> session -> get('user');

    $account = $this -> user::getLogicInstance() -> findFirstById($getUserSession['user_id']);

    $this -> view -> setVars($account['model']);
  }

  public function updateAction() {
    $resValidation = $this -> user::getValidationInstance()
    -> addPhone()
    -> addRealName()
    -> addPassword()
    -> addConfirmPassword()
    -> addSafePassword()
    -> addConfirmSafePassword()
    -> execute($this -> request -> getPost());
    if($resValidation['code'] != 0) {
      return $this -> _returnScript($resValidation['message'], '/user/profile');
    }

    $user = $this -> session -> get('user');
    $resUpdateById = $this -> user::getServiceInstance() -> updateById($user['user_id'], $resValidation['data']);
    if($resUpdateById['code'] != 0) {
      return $this -> _returnScript($resUpdateById['message'], '/user/profile');
    }

    return $this -> _returnScript('更新成功!', '/user/profile');
  }

  public function updateInfoAction() {
    $resValidation = $this -> user::getInfoValidationInstance()
      -> addSex()
      -> addAddressCodeByDistpicker()
      -> addAddressDetail()
      -> addIdentityCard()
      -> addBankName()
      -> addBankAccount()
      -> addBankAccountName()
      -> addBankDeposit()
      -> execute($this -> request -> getPost());
    if($resValidation['code'] != 0) {
      return $this -> _returnScript($resValidation['message'], '/user/profile');
    }

    $user = $this -> session -> get('user');
    $resUpdateById = $this -> user::getInfoServiceInstance() -> updateById($user['user_id'], $resValidation['data']);
    if($resUpdateById['code'] != 0) {
      return $this -> _returnScript($resUpdateById['message'], '/user/profile');
    }

    return $this -> _returnScript('更新成功!', '/user/profile');
  }
}
